package com.rabbit.mq.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
@Slf4j
public class Consumer {

    @RabbitListener(queues = "simple_queue")
    @RabbitHandler
    public void simple(Message message, Channel channel) throws Exception {
        log.info("消费端-simple: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = "work_queue")
    @RabbitHandler
    public void work1(Message message, Channel channel) throws Exception {
        log.info("消费端-work1: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = "work_queue")
    @RabbitHandler
    public void work2(Message message, Channel channel) throws Exception {
        log.info("消费端-work2: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = {"publish_subscribe_1_queue"})
    @RabbitHandler
    public void publish_subscribe_1(Message message, Channel channel) throws Exception {
        log.info("消费端-publish_subscribe_1: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = {"publish_subscribe_2_queue"})
    @RabbitHandler
    public void publish_subscribe_2(Message message, Channel channel) throws Exception {
        log.info("消费端-publish_subscribe_2: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = {"routing1_queue"})
    @RabbitHandler
    public void routing1(Message message, Channel channel) throws Exception {
        log.info("消费端-routing1: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = {"routing2_queue"})
    @RabbitHandler
    public void routing2(Message message, Channel channel) throws Exception {
        log.info("消费端-routing2: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = {"topics1_queue"})
    @RabbitHandler
    public void topics1(Message message, Channel channel) throws Exception {
        log.info("消费端-topics1: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = {"topics2_queue"})
    @RabbitHandler
    public void topics2(Message message, Channel channel) throws Exception {
        log.info("消费端-topics2: " + new String(message.getBody(), "UTF-8"));
    }

    @RabbitListener(queues = {"reply_queue"})
    @RabbitHandler
    public void reply_queue(Message message, Channel channel) throws Exception {
        log.info("消费端-reply: " + new String(message.getBody(), "UTF-8"));
        Long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
    }

    @RabbitListener(queues = {"async_queue"})
    @RabbitHandler
    public Object async_queue(Message message, Channel channel) throws Exception {
        log.info("消费端-async: " + new String(message.getBody(), "UTF-8"));
        return "ok";
    }

    //批量接收处理
    @RabbitListener(queues = {"batch_queue"})
    @RabbitHandler
    public void onMessageBatch(List<Message> messages, Channel channel) throws IOException {
        log.info("batch.queue.consumer 收到{}条message", messages.size());
        for (int i = 0; i < messages.size(); i++) {
            log.info("第" + i + "条数据是: {}", new String(messages.get(i).getBody()));
        }
        // 业务报错时需要多注意
        Long deliveryTag = messages.get(messages.size() - 1).getMessageProperties().getDeliveryTag();
        // RabbitMQ保证在每个信道中，每条消息的deliveryTag从1开始递增
        // multiple=true: 消息id<=deliveryTag的消息，都会被确认
        // multiple=false: 消息id=deliveryTag的消息，都会被确认
        channel.basicAck(deliveryTag, true);
    }
}
